<?php
declare (strict_types=1);

namespace app\controller;

use app\BaseController;
use app\model\Calendar;
use app\tool\Date;
use DateInterval;
use DateTime;
use think\facade\Log;
use think\Request;

class CalendarController extends BaseController
{
	
	protected $middleware = ['Login' => [
		'except' => ['class'],
	]];
	
	
	function index(Request $request)
	{
		$date = $request->get('date', date('Y-m'));
		list($year, $month) = explode('-', $date);
		$days = Date::days_in_month($month, $year);
		
		$arr = [];
		for ($day = 1; $day <= $days; $day++) {
			$arr[] = ['date' => $day, 'list' => Calendar::where('date', "$year-$month-$day")->where(['user_id' => $this->user_id])->select()];
		}
		$this->writeJson($arr);
	}
	
	
	/**
	 * 类目
	 * @return void
	 */
	function class()
	{
		$data = [
			['name'=>'饮食计划','color'=>'232,94,103'],
			['name'=>'运动计划','color'=>'133,146,245'],
			['name'=>'情绪控制','color'=>'188,133,245'],
			['name'=>'月经调节','color'=>'217,49,53'],
			['name'=>'备孕','color'=>'248,217,149'],
			['name'=>'冻卵准备','color'=>'123,232,100'],
		];
		$this->writeJson($data);
	}
	
	/**
	 * 创建日程
	 * @return void
	 */
	function add(Request $request)
	{
		$title = $request->post('title');
		$starttime = $request->post('starttime');
		$endtime = $request->post('endtime');
		$repeat = $request->post('repeat'); //重复:1=不重复,2=每天,3=每周,4=每月
		$warn = $request->post('warn'); //警告:1=5分钟前,2=10分钟前,3=15分钟前,4=30分钟前,5=1小时前
		$class_name = $request->post('class_name');
		$color = $request->post('color');
		Log::record('创建日程');
		Log::record($request->param());
		if (empty($color)){
			$this->writeJson(0, '颜色参数错误');
		}
		if (empty($title)) {
			$this->writeJson(0, '必须填写标题');
		}
		if (empty($class_name)) {
			$this->writeJson(0, '必须选择类目');
		}
		
		$time1 = strtotime($starttime); // 第一个时间点
		$time2 = strtotime($endtime); // 第二个时间点
		if ($time1>$time2){
			$this->writeJson(0,'开始时间不能大于结束时间');
		}
		
		if ($time2 - $time1 > 86400 ) {
			$this->writeJson(0,'开始和结束不能大于24小时');
		}
		
		switch ($warn) {
			case 1:
				$warnd = '-5 minutes';
				break;
			case 2:
				$warnd = '-10 minutes';
				break;
			case 3:
				$warnd = '-15 minutes';
				break;
			case 4:
				$warnd = '-30 minutes';
				break;
			case 5:
				$warnd = '-1 hours';
				break;
			default:
				$this->writeJson(0, '必须选择提醒');
		}
		
		
		switch ($repeat) {
			case 1:
				$data = [
					'user_id' => $this->user_id,
					'title' => $title,
					'starttime' => $starttime,
					'endtime' => $endtime,
					'warn' => $warn,
					'warntime' => strtotime($starttime . $warnd),
					'class_name' => $class_name,
					'color' => $color,
					'date' => date('Y-m-d', strtotime($starttime)),
				];
				Calendar::create($data);
				break;
			case 2:
				
				//每天  30天
				for ($i = 0; $i < 30; $i++) {
					$starttimes = date('Y-m-d H:i:s', strtotime($starttime .'+'. $i . ' days'));
					$endtimes = date('Y-m-d H:i:s', strtotime($endtime .'+'. $i . ' days'));
					
					$data = [
						'user_id' => $this->user_id,
						'title' => $title,
						'starttime' => $starttimes,
						'endtime' => $endtimes,
						'warn' => $warn,
						'warntime' => strtotime($starttimes . $warnd),
						'class_name' => $class_name,
						'color' => $color,
						'date' => date('Y-m-d', strtotime($starttimes)),
					];
					Calendar::create($data);
				}
				break;
			case 3:
				for ($i = 0; $i < 4; $i++) {
					$day = $i * 7;
					$starttimes = date('Y-m-d H:i:s', strtotime($starttime .'+'. $day . ' days'));
					$endtimes = date('Y-m-d H:i:s', strtotime($endtime .'+'. $day . ' days'));
					$data = [
						'user_id' => $this->user_id,
						'title' => $title,
						'starttime' => $starttimes,
						'endtime' => $endtimes,
						'warn' => $warn,
						'warntime' => strtotime($starttimes . $warnd),
						'class_name' => $class_name,
						'color' => $color,
						'date' => date('Y-m-d', strtotime($starttimes)),
					];
					Calendar::create($data);
				}
				break;
			case 4:
				for ($i = 0; $i < 12; $i++) {
					$starttimes = date('Y-m-d H:i:s', strtotime($starttime .'+'. $i . ' month'));
					$endtimes = date('Y-m-d H:i:s', strtotime($endtime .'+'. $i . ' month'));
					$data = [
						'user_id' => $this->user_id,
						'title' => $title,
						'starttime' => $starttimes,
						'endtime' => $endtimes,
						'warn' => $warn,
						'warntime' => strtotime($starttimes . $warnd),
						'class_name' => $class_name,
						'color' => $color,
						'date' => date('Y-m-d', strtotime($starttimes)),
					];
					Calendar::create($data);
				}
				break;
			default:
				$this->writeJson(0, '必须选择重复');
		}
		
		$this->writeJson();
	}
	
	/**
	 * 详情
	 * @param Request $request
	 * @return void
	 */
	function detail(Request $request)
	{
		$date = $request->get('date', date('Y-m-d'));
		$rows = Calendar::where(['date' => $date, 'user_id' => $this->user_id])->select();
		$this->writeJson($rows);
	}
	
	/**
	 * 删除
	 * @param Request $request
	 * @return void
	 */
	function delete(Request $request)
	{
		$id = $request->post('id/d');
		$row = Calendar::findOrFail($id);
		$row->delete();
		$this->writeJson();
	}
	
	/**
	 * 回显
	 * @param Request $request
	 * @return void
	 */
	function read(Request $request)
	{
		$id = $request->get('id/d');
		$row = Calendar::findOrFail($id);
		$this->writeJson($row);
	}
	
	/**
	 * 编辑
	 */
	function edit(Request $request)
	{
		$id = $request->post('id/d');
		$title = $request->post('title');
		$starttime = $request->post('starttime');
		$endtime = $request->post('endtime');
		$warn = $request->post('warn'); //警告:1=5分钟前,2=10分钟前,3=15分钟前,4=30分钟前,5=1小时前
		$class_name = $request->post('class_name');
		$color = $request->post('color');
		if (empty($color)){
			$this->writeJson(0, '颜色参数错误');
		}
		if (empty($title)) {
			$this->writeJson(0, '必须填写标题');
		}
		if (empty($class_name)) {
			$this->writeJson(0, '必须选择类目');
		}
	
		$time1 = strtotime($starttime); // 第一个时间点
		$time2 = strtotime($endtime); // 第二个时间点
		
		if ($time1>$time2){
			$this->writeJson(0,'开始时间不能大于结束时间');
		}
		
		
		if ($time2 - $time1 > 86400 ) {
			$this->writeJson(0,'开始和结束不能大于24小时');
		}
	
		$row = Calendar::findOrFail($id);
		switch ($warn) {
			case 1:
				$warnd = '-5 minutes';
				break;
			case 2:
				$warnd = '-10 minutes';
				break;
			case 3:
				$warnd = '-15 minutes';
				break;
			case 4:
				$warnd = '-30 minutes';
				break;
			case 5:
				$warnd = '-1 hours';
				break;
			default:
				$this->writeJson(0, '必须选择提醒');
		}

		$row->title = $title;
		$row->starttime = $starttime;
		$row->endtime = $endtime;
		$row->warn =  $warn;
		$row->warntime = strtotime($starttime . $warnd);
		$row->class_name =  $class_name;
		$row->color =  $color;
		$row->date =  date('Y-m-d', strtotime($starttime));
		$row->save();
		$this->writeJson();
	}
	
}
